package com.hpsk.shopgen.config;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * swagger 配置
 * 
 * @author 李大鹏
 */
@Slf4j
@Configuration
public class ShopGenSwaggerConfig {

    /**
     * API文档分组配置
     *
     * @author 李大鹏
     **/
    @Bean("toolsDocApiShopGenSwaggerConfig")
    public GroupedOpenApi toolsDocApi() {
        return GroupedOpenApi.builder()
                             .addOpenApiCustomizer(this::openApiCustomizer)
                             .group("代码生成")
                             .packagesToScan("com.hpsk.shopgen")
                             .addOpenApiMethodFilter(method -> {
                                 Operation operation = method.getAnnotation(Operation.class);
                                 return operation != null;
                             })
                             .build();
    }

    private void openApiCustomizer(OpenAPI openApi) {
        Contact contact = new Contact();
        contact.setName("HPSK_TEAM");
        contact.setEmail("https://www.star-new.com");
        contact.setUrl("xingyuantd@star-new.cn");
        Info info = new Info().title("代码生成")
                              .description("代码生成")
                              .contact(contact)
                              .version("2.0.0")
                              .license(new License().name("Apache 2.0").url("http://springdoc.org"));
        ExternalDocumentation externalDocumentation = new ExternalDocumentation().description("SpringShop Wiki Documentation").url("https://springshop.wiki.github.org/docs");
        openApi.setInfo(info);
        openApi.setExternalDocs(externalDocumentation);
    }
}